package com.czy.owner.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.adapter.ExpandImgAdapter;
import com.czy.owner.entity.TestingDetailsModel;
import com.czy.owner.utils.ImagePagerUtilActivity;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.widget.NoScrollRecyclerView;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hedepu on 2017-10-27
 * 车检单图片列表，
 */

public class ExpandImageView extends LinearLayout {
    private RecyclerView recyclerView;
    private ExpandImgAdapter expandImgAdapter;
    private ArrayList<String> picListRec = new ArrayList<>();

    public ExpandImageView(Context context, List<TestingDetailsModel.OwnerInspectionResModel> list) {
        super(context);
        init(context, list);
    }

    public ExpandImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ExpandImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public ExpandImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public void init(Context context, List<TestingDetailsModel.OwnerInspectionResModel> list) {
        for (TestingDetailsModel.OwnerInspectionResModel model : list) {
            if (model.getResUrl().indexOf("-src") > 0) {
                picListRec.add(model.getResUrl());
            } else {
                picListRec.add(model.getResUrl() + "-src");
            }

        }
        setOrientation(HORIZONTAL);
        TextView textView = new TextView(context);
        textView.setTextAppearance(context, R.style.CommonTextViewLeftTitle);
        textView.setText("外观图片 ");
        addView(textView);
        recyclerView = new RecyclerView(context);
        LayoutParams containerParams2 = new LayoutParams(PhoneUtils.dip2px(context, 210), LayoutParams.WRAP_CONTENT);
        recyclerView.setLayoutManager(new GridLayoutManager(context, 3));
        recyclerView.setLayoutParams(containerParams2);
        recyclerView.setFocusable(false);
        expandImgAdapter = new ExpandImgAdapter(context);
        recyclerView.setAdapter(expandImgAdapter);

        expandImgAdapter.addAll(list);
        expandImgAdapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, Object o) {


                ImagePagerUtilActivity.ImageSize imageSize = new ImagePagerUtilActivity.ImageSize(view.getMeasuredWidth(), view.getMeasuredHeight());
                ImagePagerUtilActivity.startImagePagerActivity(view.getContext(), picListRec, position, imageSize);

            }
        });
        addView(recyclerView);

    }
}
